const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const copyWebpackPlugin = require('copy-webpack-plugin');
const merge = require('webpack-merge');
const webpackDevConfig = require('./webpack.dev.config.js');
const webpackProdConfig = require('./webpack.prod.config.js');

const jsPath = 'js/';
const cssPath = 'css/';

const isDevelopment = process.env.NODE_ENV === 'development';

console.log('isDevelopment = ' + isDevelopment);

const webpackConfig = {
    entry: {
        index: './src/components/index/index.js',
        login: './src/components/login/login.js',
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: jsPath + '[name].[chunkhash:8].js'
    },
    resolve: {
        alias: {
            '@': path.resolve('src')
        }
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [path.resolve('./src'), path.resolve('./node_modules/webpack-dev-server/client')]
            },
            {
                test: /\.css$/,
                use: [
                    {
                        loader: isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
                        options: {
                            hmr: isDevelopment,
                        }
                    },
                    'css-loader',
                ]
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'images',
                    limit: 10240,
                }
            },
            {
                test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'media',
                    limit: 10240,
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'fonts',
                    limit: 10240,
                }
            }
        ]
    },
    optimization: {
        splitChunks: {
            name: 'common',
            chunks: 'all',
            filename: jsPath + 'vendors.[chunkhash:8].js'
        }
    },
    plugins: [
        /*new copyWebpackPlugin([{
            from: path.resolve(__dirname, './static'),
            to: 'static'
        }]),*/
        new webpack.ProvidePlugin({
            $: 'jquery'
        }),
        new MiniCssExtractPlugin({
            filename: cssPath + '[name].css',
            chunkFilename: cssPath + '[id].css',
            ignoreOrder: false,
        }),
        new HtmlWebpackPlugin({
            template: './src/views/index.html',
            filename: 'index.html',
            favicon: './src/assets/images/favicon.ico',
            chunks: ['common', 'index']
        }),
        new HtmlWebpackPlugin({
            template: './src/views/login.html',
            filename: 'login.html',
            favicon: './src/assets/images/favicon.ico',
            chunks: ['common', 'login']
        })
    ]
};

module.exports = (env) => {
    if (env && env.production) {
        return merge(webpackConfig, webpackProdConfig);
    }
    return merge(webpackConfig, webpackDevConfig);
};